<!-- 系统菜单编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改系统菜单':'添加系统菜单'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
                    
      <el-form-item label="父级ID:" prop="pid">
        <el-input-number
          :min="0"
          v-model="form.pid"
          placeholder="请输入父级ID"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item
        label="菜单标题:"
        prop="title">
        <el-input
          :maxlength="20"
          v-model="form.title"
          placeholder="请输入菜单标题"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="图标:"
        prop="icon">
        <el-input
          :maxlength="20"
          v-model="form.icon"
          placeholder="请输入图标"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="菜单路径:"
        prop="path">
        <el-input
          :maxlength="20"
          v-model="form.path"
          placeholder="请输入菜单路径"
          clearable/>
      </el-form-item>
                              
      <el-form-item
        label="菜单组件:"
        prop="component">
        <el-input
          :maxlength="20"
          v-model="form.component"
          placeholder="请输入菜单组件"
          clearable/>
      </el-form-item>
                                              
      <el-form-item
        label="权限标识:"
        prop="permission">
        <el-input
          :maxlength="20"
          v-model="form.permission"
          placeholder="请输入权限标识"
          clearable/>
      </el-form-item>
                      
      <el-form-item label="类型:" prop="type">
        <el-select
          clearable
          class="ele-block"
          v-model="form.type"
          placeholder="请选择类型">
                  <el-option label="菜单" :value="0"/>
                  <el-option label="节点" :value="1"/>
                </el-select>
      </el-form-item>
                      
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">禁用</el-radio>
                </el-radio-group>
      </el-form-item>
                      
      <el-form-item label="是否可见:" prop="hide">
        <el-select
          clearable
          class="ele-block"
          v-model="form.hide"
          placeholder="请选择是否可见">
                  <el-option label="显示" :value="0"/>
                  <el-option label="隐藏" :value="1"/>
                </el-select>
      </el-form-item>
                              
      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                
      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'MenuEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              pid: [
          {required: true, message: '请输入父级ID', trigger: 'blur'}
        ],
                  title: [
          {required: true, message: '请输入菜单标题', trigger: 'blur'}
        ],
                  icon: [
          {required: true, message: '请输入图标', trigger: 'blur'}
        ],
                  path: [
          {required: true, message: '请输入菜单路径', trigger: 'blur'}
        ],
                  component: [
          {required: true, message: '请输入菜单组件', trigger: 'blur'}
        ],
                                    permission: [
          {required: true, message: '请输入权限标识', trigger: 'blur'}
        ],
                  
        type: [
          {required: true, message: '请选择类型', trigger: 'blur'}
        ],
                          
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          
        hide: [
          {required: true, message: '请选择是否可见', trigger: 'blur'}
        ],
                          sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/menu/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
